/**
 *     phi
 */


var tm = tm || {};

(function(np){
    
    var DEFAULT_PARAM = {
        "emoticon"  : ":(",
        "message"   : "This browser has a problem, and need to install another browser.<br />Recommended is a <a href='http://www.google.co.jp/chrome/intl/ja/landing_ch.html'>Chrome</a> or <a href='http://mozilla.jp/firefox/'>Firefox</a>."
    };
    
    var BLUE_SCREEN_PARAM = {
        "emoticon"  : ":(",
        "message"   : "Your PC ran into a problem that it couldn't handle, and now it needs to restart."
    };
    
    var HELLO_WORLD_PARAM = {
        "emoticon"  : ":-)",
        "message"   : "Hello, world!<br />I hope you have a wonderful day.",
        "backgroundColor": "hsl(180, 60%, 50%)"
    };
    
    var HAPPY_BIRTHDAY_PARAM = {
        "emoticon"  : ":-))",
        "message"   : "Happy Birthday.<br />Your birth let us delight.",
        "backgroundColor": "hsl(70, 70%, 50%)"
    };
    
    np.destroyie6 = function(param)
    {
        // 
        // 
        // 引数の最適化
        if (typeof param == "string") {
            switch (param) {
                case "bluescreen"   : param = BLUE_SCREEN_PARAM;    break;
                case "happybirthday": param = HAPPY_BIRTHDAY_PARAM; break;
                case "helloworld"   : param = HELLO_WORLD_PARAM;    break;
            }
        }
        else if (typeof param == "undefined") {
            param = DEFAULT_PARAM;
        }
        else {
            // デフォルトパラメータで拡張
            for (var key in DEFAULT_PARAM) {
                if (param[key] == undefined) {
                    param[key] = DEFAULT_PARAM[key];
                }
            }
        }
        
        
        // 
        // 
        // destroyie6 用 スタイルシートのみを有効にする
        var styleSheet = findStyleSheet("tm.destroyie6.css");  // 保存
        destroyAllStyleSheet(); // 全てのスタイルシートを削除
        document.getElementsByTagName("head")[0].appendChild(styleSheet);   // destroyie6 用 スタイルシートを再セット
        
        
        //
        //
        // setup
        var body = document.body;
        
        body.setAttribute("id", "destroyie6");
        body.innerHTML = "";
        if (param["backgroundColor"]) body.style.backgroundColor = param["backgroundColor"];
        // wrapper
        var wrapper = document.createElement("div"); body.appendChild(wrapper);
        wrapper.className = "wrapper";
        // emoticon
        var emoticon = document.createElement("div"); wrapper.appendChild(emoticon);
        emoticon.innerHTML = param["emoticon"];
        emoticon.className = "emoticon";
        // message
        var header = document.createElement("h1"); wrapper.appendChild(header);
        header.innerHTML = param["message"];
        header.className = "message";
    };
    
    var findStyleSheet = function(filename)
    {
        var links = document.getElementsByTagName("link");
        var re = new RegExp(filename+"$");
        for (var i=0; i<links.length; ++i) {
            if (links[i].href.match(re)) {
                return links[i];
            }
        }
    };
    
    var createStyleSheet = function(filename)
    {
        var link = document.createElement("link");
        link.rel = "stylesheet";
        link.href = filename;
        link.type = "text/css";
        document.getElementsByTagName("head")[0].appendChild(link);
    };
    
    var destroyAllStyleSheet = function()
    {
        var links = document.getElementsByTagName("link");
        for (var i=0; i<links.length; ++i) {
            if (links[i].rel == "stylesheet") {
                links[i].parentNode.removeChild(links[i]);
            }
        }
    };
    
})(tm);
